package com.yunfei.api.v2;
import com.yunfei.api.v2.contants.ImagesContant;
import com.yunfei.api.v2.contants.QueryHeTongSignInfoContant;
import com.yunfei.api.v2.enums.GetAccountInfoEnum;
import com.yunfei.api.v2.param.ElecContOperLogParam;
import com.yunfei.api.v2.param.ReserveNoParam;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.enums.ResultStatus;
import com.yunfei.mapper.aiiidb.MessageSendMapper;
import com.yunfei.utils.string.StringUtil;
import com.yunfei.yunhetong.elecContOperLog.bo.ElecContOperLogBo;
import com.yunfei.yunhetong.elecContOperLog.service.ElecContOperLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 合同签署管理后台接口
 * @Author: zhengyunfei
 * @Date: 2018/6/15
 * @time:11:26
 */
@SuppressWarnings("ALL")
@RestController
@RequestMapping("/api/v2/elecContOperLog")
@Api(value = QueryHeTongSignInfoContant.OPER_LOG_NAMESPACE+ ImagesContant.IMG_STR,tags =  QueryHeTongSignInfoContant.OPER_LOG_NAMESPACE+ ImagesContant.IMG_STR )
public class ElecContOperLogApiController {
    @Autowired
    private ElecContOperLogService elecContOperLogService;
    @Autowired
    private MessageSendMapper messageSendMapper;
    /**
     * @Description: 操作日志接口
     * @Author: zhengyunfei
     * @Date: 2019/7/29
     * @time:10:30
     */
    @PostMapping(value="/add")
    @ApiOperationSort(value = 3)
    @ApiOperation(position = 3,value = QueryHeTongSignInfoContant.OPER_LOG_NAMESPACE+ ImagesContant.IMG_STR,notes = QueryHeTongSignInfoContant.OPER_LOG_NAMESPACE+ ImagesContant.IMG_STR,response = ResultModel.class)
    @ResponseBody
    public Object save(ElecContOperLogParam param, ReserveNoParam reserveNoParam){
        ResultModel resultModel=new ResultModel();
        String reserveNo=reserveNoParam.getReserveNo();
        try{
            if(StringUtil.isNullOrEmpty(reserveNo)){
                resultModel.setMessage(GetAccountInfoEnum.RESERVENO_NOTNULL.getMessage());
                resultModel.setCode(GetAccountInfoEnum.RESERVENO_NOTNULL.getCode());
                return  resultModel;
            }
            String contractId=param.getContractId();
            String contractNum=param.getContractNum();
            String operId=param.getOperId();
            String operTyp=param.getOperTyp();
            if(StringUtil.isNullOrEmpty(operTyp)){
                operTyp="签署合同";
            }
            //记录操作日志
            ElecContOperLogBo elecContOperLogBo=new ElecContOperLogBo();
            elecContOperLogBo.setContractId(contractId);
            elecContOperLogBo.setContractNum(contractNum);
            elecContOperLogBo.setOperId(operId);
            elecContOperLogBo.setOperTyp(operTyp);
            //首先查询日志是否存在
            ElecContOperLogBo queryParam=new ElecContOperLogBo();
            queryParam.setContractNum(contractNum);
            queryParam.setContractId(contractId);
            queryParam.setOperId(operId);
            int count=elecContOperLogService.count(queryParam);
            if(count==0){
                elecContOperLogService.add(elecContOperLogBo);
                resultModel.setData("日志写入成功");
                resultModel.setCode(ResultStatus.SUCCESS.getCode());
                resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
                //给理财师发送短信
                Map<String,Object> sendMap=new HashMap<>();
                sendMap.put("reserveNo",reserveNo);
                sendMap.put("contractId",contractId);
                messageSendMapper.send(sendMap);
            }else{
                resultModel.setData("日志已存在");
                resultModel.setCode(ResultStatus.SUCCESS.getCode());
                resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
            }
            return resultModel;
        }catch (Exception e){
            e.printStackTrace();
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }


}
